<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use App\Model\VideoCategory;
class Video extends Model
{

    protected $table = 'videos';
    protected $guarded = [];
    
	//一个视频对应一个分类
    public function video_category()
    {
        return $this->belongsTo('App\Model\VideoCategory','video_category_id');
    }

    public function video_url()
    {
        return $this->hasMany('App\Model\VideoUrl','video_id');
    }

    /**
     * 检查是否有子栏目
     */
    static function check_children($id)
    {
        $category = self::with('video_url')->find($id);
        if ($category->video_url->isEmpty()) {
            return true;
        }
        return false;
    }

    /**
     * 检查是否有子栏目
     */
    static function destroy_checked_children($id)
    {
        $category = self::with('video_url')->find($id);
        if ($category->video_url->isEmpty()) {
            return true;
        }
        return false;
    }

    //添加文件夹
   static public function add_local_upload()
    {
        //上传文件夹，如果不存在，建立文件夹
        $categories=VideoCategory::select('name')->get();
        //$categories=array('电影','电视剧','综艺');
        //return $categories;
        if($categories){
            foreach ($categories as $category){
                //$path = getcwd() . '/finder/upload/' . $category->name;
                $path = getcwd() . '/finder/upload/' . $category->name;
                if (!is_dir($path)) {
                    @mkdir(mkdir($path,0777,true));
                }
            }

        }
    }

}
